<template>
  <div>
    <el-card style="overflow:visible;">
      <el-form :model="form" size="mini" label-position="top" ref="form">
        <el-row :gutter="10">
          <el-col :lg="4" :md="6">
            <el-form-item label="机构" prop="BRANCHNO">
              <select-tree  :branch="form.branchName" @getBranchName="getBranch"></select-tree>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label="班次事件编号" prop="classId">
              <el-input v-model="form.classId" placeholder="请输入班次事件编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="6">
            <el-form-item label="班次事件名称" prop="className">
              <el-input v-model="form.className" placeholder="请输入班次事件名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg='8' :md='12' class="searchBtnMarginTop">
            <el-button type="primary" icon="el-icon-search" @click="classEventQuery" size='mini'>查询
            </el-button>
          <!-- </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop"> -->
            <el-button type="warning" size="mini" icon="el-icon-refresh" @click="resetForm('form')">重置</el-button>
          <!-- </el-col>
          <el-col :lg="2" :md="3" class="searchBtnMarginTop"> -->
            <el-button type="primary" size="mini" @click="addDialog" icon="el-icon-plus">新增班次事件</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
        <el-col>
          <el-table :data="tableData" style="margin-top:20px;">
            <el-table-column label="事件编号" prop="CLASSESEVENTID" width="130"></el-table-column>
            <el-table-column label="事件名称" prop="CLASSESEVENTNAME"></el-table-column>
            <el-table-column label="活动编号" prop="ACTIVITYTYPE"></el-table-column>
            <el-table-column label="活动类型" prop="ACTIVITYTYPENAME"></el-table-column>
            <el-table-column label="时长" prop="HOURS" align="center"></el-table-column>
            <el-table-column label="开始时间类型" align="center" prop="SRTTIMETYPE" width="160">
              <template slot-scope="scope">
                <span>{{scope.row.SRTTIMETYPE === "0"?"绝对时间":(scope.row.SRTTIMETYPE === "1"?"相对于班次开始时间后":(scope.row.SRTTIMETYPE === "2"?"相对于班次结束时间前":"任意时间"))}}</span>
              </template>
            </el-table-column>
            <el-table-column label="时间" prop="SRTTIME" width="120" align="center">
              <template slot-scope="scope">
                <div>
                  {{scope.row.SRTTIME}}
                  <span v-if="scope.row.ENDTIME != ''"> -- {{scope.row.ENDTIME}}</span>
                </div>
              </template>
            </el-table-column>
            <!-- <el-table-column label="结束时间" prop="ENDTIME" width="100" align="center"></el-table-column> -->
            <el-table-column label="是否带薪" prop="ISPAID" align="center">
              <template slot-scope="scope">
                <span>{{scope.row.ISPAID === "1"?"是":"否"}}</span>
              </template>
            </el-table-column>
            <el-table-column label="最少次数" prop="MINTIMES" width="100" align="center"></el-table-column>
            <el-table-column label="最多次数" prop="MAXTIMES" width="100" align="center"></el-table-column>
            <el-table-column label="说明" prop="NOTES" width="120"></el-table-column>
            <el-table-column label="操作" fixed="right">
              <template slot-scope="scope">
                <el-tooltip class="item" effect="dark" content="修改" placement="top">
                  <el-button type="text" style="margin-left:0px" @click="editDialog(scope.row)">
                    <i class="el-icon-edit-outline"></i>
                  </el-button>
                </el-tooltip>
                <el-tooltip class="item" effect="dark" content="删除" placement="top">
                  <el-button type="text" style="margin-left:0px" @click="delEvent(scope.row)">
                    <i class="el-icon-delete"></i>
                  </el-button>
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
        <el-col>
          <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange"
            :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="tableTotal">
          </el-pagination>
        </el-col>
      </el-row>
      <!-- <el-row>
        <el-col>
          <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange" :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="InTotalNum">
          </el-pagination>
        </el-col>
      </el-row> -->
      <component :is="dialogList.editDialog" :option="editOptions" @ListenClass="classEventQuery"></component>
    </el-card>
  </div>
</template>
<script>
  import request from "@/common/utils/requestWFM";
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import selectTree from "@/components/selectTree5/";
  import Query from '@/message/wfm/scheduleDataManage/classEventManage/classEventQuery'
  import Del from '@/message/wfm/scheduleDataManage/classEventManage/classEventDel'
  import Update from '@/message/wfm/scheduleDataManage/classEventManage/classEventUpdate'
  import Add from '@/message/wfm/scheduleDataManage/classEventManage/classEventAdd'
  import editDialog from './dialog/editDialog'

  export default {
    components: {
      selectTree
    },
    mounted() {
      this.classEventQuery()
    },
    data() {
      return {
        dialogList: {
          editDialog
        },
        editOptions: {
          isShow: false,
          title: '',
          row: {},
        },
        popoverShow: false,
        startTime0: '',
        startTime1: '',
        startTime2: '',
        endTime: '',
        actOptions: [], //活动类型
        tableTotal: 1,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        form: { //表单数据
          classId: "",
          className: "",
          branchNO: this.$store.getters.BranchNum,
          branchName: this.$store.getters.BranchName,
        },
        InTotalNum: 10,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        ruleForm: {
          branchName: this.$store.getters.BranchName,
          branchNO: this.$store.getters.BranchNum,
          CLASSESEVENTID: '',
          CLASSESEVENTNAME: '',
          NOTES: '',
          ACTIVITYTYPE: '',
          HOURS: '',
          SRTTIMETYPE: '',
          SRTTIME: '',
          ENDTIME: '',
          ISPAID: '0',
          MINTIMES: '1',
          MAXTIMES: '1'
        },
        rules: {
          SRTTIMETYPE: [{
            required: true,
            message: '请选择开始时间类型',
            trigger: 'change'
          }],
          branchName: [{
            required: true,
            message: '请选择机构',
            trigger: 'change'
          }],
          CLASSESEVENTNAME: [{
              required: true,
              message: '请输入班次事件名称',
              trigger: 'blur'
            },
            {
              min: 1,
              max: 25,
              message: '长度在 1 到 25 个字符',
              trigger: 'blur'
            }
          ],
          HOURS: [{
            required: true,
            message: '请选择时长',
            trigger: 'change'
          }],
          ISPAID: [{
            required: true,
            message: '请选择是否带薪',
            trigger: 'change'
          }],
          ACTIVITYTYPE: [{
            required: true,
            message: '请选择活动类型',
            trigger: 'change'
          }],
          NOTES: [{
            min: 1,
            max: 250,
            message: '长度在 1 到 250 个字符',
            trigger: 'blur'
          }],
        },
        dialogTitle: '',
        editIsShow: false,
        input: '',
        select: '',
        tableData: [],
        FIELDNAME: ''
      }
    },
    methods: {
      resetForm(formName) {
        if(this.$refs[formName] != undefined){
          this.$refs[formName].clearValidate();
          this.$refs[formName].resetFields()
        }
      },
      getBranch(data) {
        // console.log(data)
        this.form.branchName = data.BRANCHNAME
        this.form.branchNO = data.BRANCHNO
        this.ruleForm.branchNO = data.BRANCHNO
        this.popoverShow = false
      },
      //前端分页
      handleInCurrentChange(val) {
        this.currpage = val;
        // console.log(`当前页: ${val}`);
      },
      handleInSizeChange(val) {
        this.pageSize = val;
        this.classEventQuery();
      },
      // 班次事件查询
      async classEventQuery() {
        let resBody = new Query()
        resBody.data.BRANCHNO = this.form.branchNO
        resBody.data.CLASSESEVENTID = this.form.classId
        resBody.data.CLASSESEVENTNAME = this.form.className
        resBody.data.CurrPage = this.currpage;
        resBody.data.PageRowNum = this.pageSize;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.RSPSTRUCT
          this.tableTotal = response.BODY.TotalRowNum;
          this.form.branchName = response.BODY.BRANCHNAME
          this.form.branchNO = response.BODY.BRANCHNO
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 班次事件删除
      delEvent(row) {
        this.$confirm('此操作将删除该班次事件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let resBody = new Del()
          resBody.data.CLASSESEVENTID = row.CLASSESEVENTID
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.classEventQuery()
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      },
      // //前端分页
      // handleInCurrentChange(val) {
      //   this.currpage = val;
      //   this.stopWordQuery();
      //   // console.log(`当前页: ${val}`);
      // },
      // handleInSizeChange(val) {
      //   this.pageSize = val;
      //   this.stopWordQuery();
      // },
      addDialog() {
       this.editOptions.isShow = true
        this.editOptions.title = '新增班次事件'
      },
      editDialog(row) {
        this.editOptions.isShow = true
        this.editOptions.title = '修改班次事件'
        this.editOptions.row = row
        this.editOptions.row.branchNO = this.form.branchNO
        this.editOptions.row.branchName = this.form.branchName
        // if (this.ruleForm.SRTTIMETYPE == '0') {
        //   this.startTime0 = this.ruleForm.SRTTIME
        //   this.endTime = this.ruleForm.ENDTIME
        // } else if (this.ruleForm.SRTTIMETYPE == '1') {
        //   this.startTime1 = this.ruleForm.SRTTIME
        // } else if (this.ruleForm.SRTTIMETYPE == '2') {
        //   this.startTime2 = this.ruleForm.SRTTIME
        // }
        // this.actQuery()
      },
    }
  }

</script>
